package com.itheima.goodsconsumer.api;

import com.itheima.goodsconsumer.VO.ResultVO;
import com.itheima.goodsconsumer.constant.ResultEnum;
import com.itheima.goodsconsumer.model.Cart;
import com.itheima.goodsconsumer.model.Goods;
import com.itheima.goodsconsumer.model.Userorder;
import com.itheima.goodsconsumer.service.GoodsService;
import com.itheima.goodsconsumer.util.ResultVOUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api/goods")
public class GoodsAPI {
    @Autowired
    GoodsService goodsService;

    /**
     * 退出登录
     *
     * @param session
     * @return
     */
    @GetMapping(value = "/quit")
    public String quit(HttpSession session) {
        session.removeAttribute("uaccount");
        return "redirect:http://localhost:8893/admin/tologin";
    }

    /**
     * 首页查询全部商品显示
     *
     * @return
     */
    @GetMapping(value = "/getAll")
    public ResultVO getAll() {

        List<Goods> goods1 = new ArrayList<>();
        List<Goods> goods2 = new ArrayList<>();
        List<Goods> goods3 = new ArrayList<>();
        List<Goods> goods4 = new ArrayList<>();

        List<Goods> oldgoods = goodsService.getAllgoods();
        for (Goods newgoods : oldgoods) {
            System.out.print(newgoods.getTypes());
            if (newgoods.getTypes().equals(1)) {

                goods1.add(newgoods);
            }
            if (newgoods.getTypes().equals(2)) {
                goods2.add(newgoods);
            }
            if (newgoods.getTypes().equals(0)) {
                goods3.add(newgoods);
            }
            if (newgoods.getTypes().equals(3)) {
                goods4.add(newgoods);
            }
        }

        List<List<Goods>> goodsList = new ArrayList<>();
        goodsList.add(goods1);
        goodsList.add(goods2);
        goodsList.add(goods3);
        goodsList.add(goods4);
        return ResultVOUtil.success(goodsList);
    }

    /**
     * 商品详情-即根据ID查询单个商品
     *
     * @param request
     * @param gid
     * @return
     */
    @GetMapping(value = "/detail")
    public ResultVO detail(HttpServletRequest request, int gid) {
        //request.setAttribute("detail", goodsService.getOnegid(gid));
        Goods goods = goodsService.getOnegid(gid);
        return ResultVOUtil.success(goods) ;
    }

    /**
     * 商品首页搜索-根据名称模糊查找商品
     *
     * @param request
     * @param name
     * @return
     */
    @GetMapping(value = "/getOne")
    public ResultVO getOne(HttpServletRequest request, String name) {
        //request.setAttribute("goods",goodsService.getOnegoods(name));
        List<Goods> goods1 = new ArrayList<>();
        List<Goods> goods2 = new ArrayList<>();
        List<Goods> goods3 = new ArrayList<>();
        List<Goods> goods4 = new ArrayList<>();

        List<Goods> oldgoods = goodsService.getOnegoods(name);
        System.out.print(oldgoods + "........................................");
        for (Goods newgoods : oldgoods) {
            System.out.print(newgoods.getTypes());
            if (newgoods.getTypes().equals(1)) {

                goods1.add(newgoods);
            }
            if (newgoods.getTypes().equals(2)) {
                goods2.add(newgoods);
            }
            if (newgoods.getTypes().equals(0)) {
                goods3.add(newgoods);
            }
            if (newgoods.getTypes().equals(3)) {
                goods4.add(newgoods);
            }
        }

        List<List<Goods>> goodsList = new ArrayList<>();
        goodsList.add(goods1);
        goodsList.add(goods2);
        goodsList.add(goods3);
        goodsList.add(goods4);
        return ResultVOUtil.success(goodsList);
    }

    /**
     * 删除购物车，根据id删除购物车商品（即购买之后删除购物车商品）
     *
     * @param request
     * @param did
     * @return
     */
    @GetMapping(value = "/deleteCart")
    public ResultVO deleteCart(HttpServletRequest request, String did) {
        Integer result = goodsService.deleteCart(Integer.parseInt(did));
        if (result == 1) {
            return ResultVOUtil.success();
        } else {
            return ResultVOUtil.error(ResultEnum.DELETE_FAIL);
        }
        //return "redirect:/goods/cartAll";
    }


    @GetMapping(value = "/paygoods")
    public String paygoods(HttpServletRequest request, String[] goodid, String[] numaa, HttpSession session, String[] id) {
        String uid = (String) session.getAttribute("uaccount");
        if (goodid.length == 0 || numaa.length == 0 || id.length == 0) {
            return "redirect:/goods/cartAll";
        } else {
            for (int i = 0; i < goodid.length; i++) {
                goodsService.insertOrder(goodsService.getOnegid(Integer.parseInt(goodid[i])).gname, Integer.parseInt(numaa[i]), Integer.parseInt(numaa[i]) * goodsService.getOnegid(Integer.parseInt(goodid[i])).gprice, Integer.parseInt(uid));
                // goodsService.insertOrder(goodsService.getOnegid(Integer.parseInt(goodid)),Integer.parseInt(numaa[i]),Integer.parseInt(numaa[i])*goodsService.getOnegid(Integer.parseInt(gid[i])).gprice);
                goodsService.deleteCart(goodsService.deleteCart(Integer.parseInt(id[i])));
            }
            request.setAttribute("mag3", "购买失败");
            return "redirect:/goods/getAllorder";
        }
    }

    /**
     * 根据用户查出用户下所有订单
     *
     * @param request
     * @param session
     * @return
     */
    @GetMapping(value = "/getAllorder")
    public ResultVO getAllorder(HttpServletRequest request, HttpSession session) {
        String uid = (String) session.getAttribute("uaccount");
        List<Userorder> userorders = goodsService.getAllorder(Integer.parseInt(uid));
        request.setAttribute("order", goodsService.getAllorder(Integer.parseInt(uid)));
//        if (uid.equals(null)) {
//            return "redirect:http://localhost:8896/admin/tologin";
//        }
//        return "order";
        return ResultVOUtil.success(userorders);

    }

    /**
     * 查询购物车全部商品
     *
     * @param request
     * @param session
     * @return
     */
    @GetMapping(value = "/cartAll")
    public ResultVO cartAll(HttpServletRequest request, HttpSession session) {
        String uid = (String) session.getAttribute("uaccount");

        //request.setAttribute("carts", goodsService.getAllcart(Integer.parseInt(uid)));
        //       return "Settle";
        List<Cart> usercart = goodsService.getAllcart(Integer.parseInt(uid));
        return ResultVOUtil.success(usercart);

    }

    /**
     * 加入购物车-将商品加入购物车并查询出全部商品如果有商品修改商品数量
     *
     * @param request
     * @param name
     * @param price
     * @param che
     * @param number
     * @param session
     * @return
     */
    @GetMapping(value = "/cart")
    public ResultVO cart(HttpServletRequest request, String name, String price, String che, String number, HttpSession session) {
        String uid = (String) session.getAttribute("uaccount");
        List<Cart> usercart = goodsService.getAllcart(Integer.parseInt(uid));
        int n = Integer.parseInt(price);
        System.out.print(number);
        for (int i = 0; i < usercart.size(); i++) {
            System.out.print(usercart.get(i).getGoodid());
            if (usercart.get(i).getGoodid() == Integer.parseInt(che)) {
                Integer result =goodsService.updateCart(Integer.parseInt(number), usercart.get(i).getId());
                //return "redirect:/goods/cartAll";
                ResultVOUtil.success(result);
            }
        }
        Integer  result2 = goodsService.intcart(name, Integer.parseInt(number), n, Integer.parseInt(che), Integer.parseInt(uid));
        //return "redirect:/goods/cartAll";
        return ResultVOUtil.success(result2);
    }


    /**
     *加入购物车
     * @param request
     * @param che
     * @return
     */
   /* @GetMapping(value = "shop")
    public String shop(HttpServletRequest request,String[] che){
        List<Goods> newgoods= new ArrayList<Goods>();
        if(che ==null ){
            System.out.print("不好意思你没有选中商品，请选中后再加购物车！！");
            return "redirect:/goods/getAll";
        }else {
            for (int i = 0; i < che.length; i++) {
                Goods n = goodsService.getOnegid(Integer.parseInt(che[i]));
                newgoods.add(n);
            }
            request.setAttribute("newgoods", newgoods);
            return "Settle";
        }
    }*/

   /*   @GetMapping(value = "/settle")
        public String tosettle() {
        return "Settle";
    }*/
}
